/**
 * Created by meiguizhinian on 2018/7/16
 * Copyright © 2018 lixiaolong. All rights reserved.
 */

//TODO：等待完成
(function () {
	ivy.SpineAnimationData = function (param) {
		//成员变量
		this.spAtlas_ = null;//spAtlas*
		this.spSkeletonData_ = null;//spSkeletonData*
		this.actionNameFindMap_ = new LLDictionary();//std::unordered_map<int32_t, std::string>
		this.actionIndexFindMap_ = new LLDictionary();//std::unordered_map<std::string, int32_t>
		this.actionCollisionRectMap_ = new LLDictionary();//std::unordered_map<std::string, cocos2d::Rect>
		this.actionAttackRectMap_ = new LLDictionary();//std::unordered_map<std::string, cocos2d::Rect>
	};
	let p = ivy.SpineAnimationData.prototype;
	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	p.getSkeletonData = function() {
		return this.spSkeletonData_;
	};

	p.setSkeletonData = function(data) {
		this.spSkeletonData_ = data;
	};

	p.getAtlas = function() {
		return this.spAtlas_;
	};

	p.setAtlas = function(data) {
		this.spAtlas_ = data;
	};

	p.getActionCount = function() {
		return this.actionIndexFindMap_.size();
	};

	p.getActionIndexByName = function(name) {
		return this.actionIndexFindMap_.get(name) || -1;
	};

	p.getActionNameByIndex = function(i) {
		return this.actionNameFindMap_.get(i) || "";
	};

	p.addSoundTimeLineData = function(filePath) {
		if (this.spSkeletonData_) {
			let is = new ivy.InputStream(filePath);
			if (is.empty()) {
				return;
			}
			let actionCount = is.readJInt16();
			for (let i = 0; i < actionCount; ++i) {
				cc.assert(false,"addSoundTimeLineData");
				let actionName = is.readStringUTF8();
				let soundCount = is.readJInt16();
				if (soundCount <= 0)
					continue;
				if (soundCount > 0) {
					// let timeline = spSoundTimeline_create(soundCount);
					// timeline.slotIndex = 0;
					// for (let j = 0; j < soundCount; ++j)
					// {
					// 	let str = is.readStringUTF8();
					// 	let soundIndex = is.readJInt16();
					// 	timeline.frames[j] = Number(str);
					// 	timeline.soundIdxs[j] = soundIndex;
					// }
					//let action = spSkeletonData_findAnimation(spSkeletonData_, actionName.c_str());
					//let addedCount = action.timelinesCount + 1;
					//spTimeline** newTimeLine = MALLOC(spTimeline*, addedCount);
					//memcpy(newTimeLine, action.timelines, sizeof(spTimeline*) * action.timelinesCount);
					//newTimeLine[action.timelinesCount] = SUPER_CAST(spTimeline, timeline);
					//FREE(action.timelines);
					//action.timelinesCount = addedCount;
					//action.timelines = newTimeLine;
				}
			}
			let actionExCount = is.readJInt16();
			for (let i = 0; i < actionExCount; ++i)
			{
				let actionName = is.readStringUTF8();
				this.actionIndexFindMap_.set(actionName, i);
				this.actionNameFindMap_.set(i, actionName);
				let ax0 = is.readJInt16();
				let ay0 = is.readJInt16();
				let ax1 = is.readJInt16();
				let ay1 = is.readJInt16();
				this.actionAttackRectMap_.set(actionName, cc.rect(ax0, ay0, ax1 - ax0, ay1 - ay0));
				let cx0 = is.readJInt16();
				let cy0 = is.readJInt16();
				let cx1 = is.readJInt16();
				let cy1 = is.readJInt16();
				this.actionCollisionRectMap_.set(actionName, cc.rect(cx0, cy0, cx1 - cx0, cy1 - cy0));
			}
		}
	};
	p.getAttackBox = function(actionID, seqID) {
		if (this.actionNameFindMap_.has(actionID)) {
			let name = this.actionNameFindMap_.get(actionID);
			return cc.rect(this.actionAttackRectMap_.get(name));
		}
		return cc.rect();
	};
	p.getCollisionBox = function(actionID, seqID) {
		if (this.actionNameFindMap_.has(actionID)) {
			let name = this.actionNameFindMap_.get(actionID);
			return cc.rect(this.actionCollisionRectMap_.get(name));
		}
		return cc.rect();
	}
})();

ivy.SpineAnimationData.createWithFileName = function(fileName) {
	let jsonFile = ivy.GlobleConfig.getInstance().getSpineJsonDataPath(fileName);
	let atlasFile = ivy.GlobleConfig.getInstance().getSpineAtlasDataPath(fileName);
	let soundFile = ivy.GlobleConfig.getInstance().getSpineAnimationDataPath(fileName);

	jsonFile = cc.FileUtils.getInstance().fullPathForFilename(jsonFile);
	atlasFile = cc.FileUtils.getInstance().fullPathForFilename(atlasFile);
	soundFile = cc.FileUtils.getInstance().fullPathForFilename(soundFile);

	if (cc.FileUtils.getInstance().isFileExist(atlasFile)) {
		var data = cc.loader.getRes(atlasFile);
		sp._atlasLoader.setAtlasFile(atlasFile);
		let atlas = new spine.TextureAtlas(data, sp._atlasLoader.load.bind(sp._atlasLoader));
		if (atlas) {
			let sptr = new ivy.SpineAnimationData();
			sptr.setAtlas(atlas);

			let attachmentLoader = new spine.AtlasAttachmentLoader(atlas);
			//json
			var skeletonJsonReader = new spine.SkeletonJson(attachmentLoader);
			skeletonJsonReader.scale = 1;

			var skeletonJson = cc.loader.getRes(jsonFile);
			let skeletonData = skeletonJsonReader.readSkeletonData(skeletonJson);
			if (skeletonData) {
				atlas.dispose(skeletonJsonReader);
				sptr.setSkeletonData(skeletonData);
				sptr.addSoundTimeLineData(soundFile);
				return sptr;
			}
		}
	}
	return null;
};